package com.zqxq.lottery.controller.sys;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.zqxq.common.constant.Result;
import com.zqxq.common.enums.CodeEnum;
import com.zqxq.lottery.entity.o2o.sys.SysConfigO2O;
import com.zqxq.lottery.entity.po.sys.SysConfigPO;
import com.zqxq.lottery.entity.vo.sys.SysConfigVO;
import com.zqxq.lottery.mapper.sys.SysConfigMapper;
import com.zqxq.lottery.service.sys.ISysConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;


@RestController
@Slf4j
@RequestMapping("/sysConfig")
public class SysConfigController extends SysBaseController{
    @Autowired
    private ISysConfigService sysConfigService;
    @Autowired
    private SysConfigMapper sysConfigMapper;
    @PostMapping("/queryPageList")
    public Result queryConfigValue(String appKey,String configKey) {
        Result result = new Result();
        SysConfigPO config = sysConfigMapper.getByKey(configKey);
        if (!StringUtils.isEmpty(appKey)) {
            SysConfigPO mjConfig = sysConfigMapper.getByKey("APP_KEY");
            if (mjConfig != null) {
                Map map = JSONObject.parseObject(mjConfig.getCval(), Map.class);
                String value = String.valueOf(map.get(appKey));
                if (value.equals("0") && (configKey.equals("MY_QUIZ_SWITCH") || configKey.equals("GUESS_COMPETITION"))) {
                    config.setCval("0");
                }
            }
        }
        result.setData(config);
        return result;
    }

    @PostMapping("/getSysConfigList")
    public Result getSysConfigList(@RequestBody Map<String,Object> paramMap) {
        Result result = new Result();
        Page page = sysConfigService.getSysConfigList(paramMap);
        result.setData(page);
        return result;
    }

    @PostMapping("/update")
    public Result update(@RequestBody SysConfigVO vo){
        Result result = new Result();
        try{
            SysConfigPO po = SysConfigO2O.toPO(vo);
            sysConfigService.insertOrUpdate(po);
        } catch (Exception e){
            result.setCode(CodeEnum.FAIL_100001.getCode());
            result.setMsg(CodeEnum.FAIL_100001.getMsg());
        }
        return result;
    }

}
